package com.solo.system.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.solo.common.core.global.R;
import com.solo.common.logger.annotation.Logger;
import com.solo.common.logger.enums.LoggerType;
import com.solo.system.model.user.req.AllocatedUserReq;
import com.solo.system.model.user.req.AssignRoleReq;
import com.solo.system.model.user.req.AssignUserReq;
import com.solo.system.model.user.req.UnallocatedUserReq;
import com.solo.system.model.user.resp.AllocatedUserResp;
import com.solo.system.model.user.resp.UnallocatedUserResp;
import com.solo.system.model.user.resp.UserRoleResp;
import com.solo.system.service.SysUserRoleService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import static com.solo.system.api.entity.table.SysUserRoleTableDef.SysUserRoleTable;

/**
 * 用户角色关联控制器
 * @author 十一
 * @since 2024-10-16 17:18
 * 人生若只如初见，何事秋风悲画扇
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/user-role")
public class SysUserRoleController {

    private final SysUserRoleService sysUserRoleService;

    /**
     * 取消指定角色的用户授权
     * @param userIds 用户ID集合
     * @return {@link R }<{@link Boolean }>
     */
    @DeleteMapping("/delete-user/{roleId}/{userIds}")
    @SaCheckPermission("system-role-user-assign")
    @Logger(value = "取消用户授权", type = LoggerType.DELETE)
    public R<Boolean> delete(@PathVariable("roleId") Long roleId, @PathVariable("userIds") Long[] userIds) {
        QueryWrapper qw = QueryWrapper.create()
                .where(SysUserRoleTable.RoleId.eq(roleId))
                .and(SysUserRoleTable.UserId.in(userIds));
        return R.success(sysUserRoleService.remove(qw));
    }

    /**
     * 分配角色
     * @param req 用户分配角色请求对象
     * @return {@link R }<{@link Boolean }>
     */
    @PutMapping("/assign-role")
    @SaCheckPermission("system-role-user-assign")
    @Logger(value = "分配角色", type = LoggerType.UPDATE)
    public R<Boolean> assignRole(@Valid @RequestBody AssignRoleReq req) {
        return R.success(sysUserRoleService.assignRole(req));
    }

    /**
     * 分配用户
     * @param req 请求对象
     * @return {@link R }<{@link Boolean }>
     */
    @PostMapping("/assign-user")
    @SaCheckPermission("system-role-user-assign")
    @Logger(value = "分配用户", type = LoggerType.CREATE)
    public R<Boolean> assignUser(@Valid @RequestBody AssignUserReq req) {
        return R.success(sysUserRoleService.assignUser(req));
    }

    /**
     * 查询已分配角色的用户分页列表
     * @param page 分页对象
     * @param req 请求对象
     * @return 响应对象
     */
    @GetMapping("/allocated-user")
    @SaCheckPermission("system-role-user-assign")
    public R<Page<AllocatedUserResp>> allocatedUser(Page<AllocatedUserResp> page, AllocatedUserReq req) {
        return R.success(sysUserRoleService.queryAllocatedUserPage(page, req));
    }

    /**
     * 查询未分配角色的用户分页列表
     * @param page 分页对象
     * @param req 请求对象
     * @return {@link R }<{@link Page }<{@link UnallocatedUserResp }>>
     */
    @GetMapping("/unallocated-user")
    @SaCheckPermission("system-role-user-assign")
    public R<Page<UnallocatedUserResp>> unallocatedUser(Page<UnallocatedUserResp> page, UnallocatedUserReq req) {
        return R.success(sysUserRoleService.queryUnallocatedUserPage(page, req));
    }

    /**
     * 查询用户角色
     * @param userId 用户id
     * @return {@link R }<{@link UserRoleResp }>
     */
    @GetMapping("/query/{userId}")
    @SaCheckPermission("system-role-user-assign")
    public R<UserRoleResp> queryUserRole(@PathVariable Long userId) {
        return R.success(sysUserRoleService.queryUserRole(userId));
    }

}
